//
//  ViewController.m
//  ImageCapture
//
//  Created by 蜂巢网络科技 on 2017/8/16.
//  Copyright © 2017年 fengchao. All rights reserved.
//

#import "ViewController.h"
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "YYImage.h"
@interface ViewController ()
@property (nonatomic, retain) YYImageDecoder *decoder;
@property (nonatomic, assign) int i;
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    

}

#pragma mark - 两种加载方式
- (void)firstLoadImage
{
    NSMutableArray *imageArray1 = [NSMutableArray array];
    for (int i = 0 ; i < 18; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        [imageArray1 addObject:image];
    }
    imageArray1 = nil;
}

- (void)secondImage
{
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 0 ; i < 18; i++) {
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i] ofType:@"png"];
        
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [imageArray addObject:image];
    }
    imageArray = nil;
}

#pragma mark - 新图片格式
- (void)newImageFormat
{
    NSURL *webstr = [[NSBundle mainBundle] URLForResource:@"be" withExtension:@"webp"];
    NSData *data = [NSData dataWithContentsOfURL:webstr];
    _decoder = [YYImageDecoder decoderWithData:data scale:1.0];
    self.i = 0;
    UIImage *image = [_decoder frameAtIndex:self.i decodeForDisplay:YES].image;
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)info
{
    if (error!=NULL) {
        NSLog(@"error = %@",error);
    }else{
        NSLog(@"success");
        self.i++;
        UIImage *image = [_decoder frameAtIndex:self.i decodeForDisplay:YES].image;
        if (image!=nil) {
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }
}

#pragma mark - gif合成
- (void)gifCompose
{
    // 1.获取数据
    NSMutableArray *images = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"0"],[UIImage imageNamed:@"1"],[UIImage imageNamed:@"2"],[UIImage imageNamed:@"3"],[UIImage imageNamed:@"4"],[UIImage imageNamed:@"5"],nil];
    
    // 2.创建gif文件
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentStr = [document objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *textDic = [documentStr stringByAppendingString:@"/2"];
    [fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [textDic stringByAppendingString:@"test.gif"];
    NSLog(@"path = %@",path);
    
    // 3.配置gif属性
    CGImageDestinationRef destion;
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, NULL);
    NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil] forKey:(NSString *)kCGImagePropertyGIFDelayTime];
    NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];
    [gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
    [gifParmdict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
    [gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
    [gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    // 4.单帧添gif
    for (UIImage *dimage in images) {
        CGImageDestinationAddImage(destion, dimage.CGImage, (__bridge CFDictionaryRef)frameDic);
    }
    CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
    CGImageDestinationFinalize(destion);
    CFRelease(destion);
}

#pragma mark - gif动画展示
- (void)gifShow
{
    NSMutableArray *imageTemp = [NSMutableArray array];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 300, 200)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
    
    for (int i = 0 ; i < 6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        [imageTemp addObject:image];
    }
    [imageView setAnimationImages:imageTemp];
    [imageView setAnimationRepeatCount:10];
    [imageView setAnimationDuration:2];
    [imageView startAnimating];
}

#pragma mark - gif图片分解
- (void)gifResolve
{
    // 1.获取gif数据
    NSString *path = [[NSBundle mainBundle]pathForResource:@"bee" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    
    // 2.将图片分解为一帧一帧
    size_t count = CGImageSourceGetCount(source);
    
    NSMutableArray *tempArray = [NSMutableArray array];
    for (size_t i = 0 ; i < count; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        
        // 3.将单帧数据转为UIImage
        UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [tempArray addObject:image];
        
        CGImageRelease(imageRef);
        
    }
    CFRelease(source);
    
    // 4.单帧图片保存
    int i = 0;
    for (UIImage *image in tempArray) {
        NSData *data = UIImagePNGRepresentation(image);
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *gifPath = path[0];
        NSString *pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
        NSLog(@"%@",pathNum);
        [data writeToFile:pathNum atomically:NO];
        
        i++;
    }
}

@end
